package co.infinum.goldfinger.example.widget;

import co.infinum.goldfinger.example.ResourceTable;
import ohos.agp.components.*;
import ohos.app.Context;

/**
 * ex.
 *
 * @author zh
 * @since 2021-06-25
 */
public class SwitchCompat extends ComponentContainer {
    private Context mContext;
    private Component contentView;
    private Text title;
    private Switch switchButton;
    private boolean isCheched;
    private OnCheckedChangeListener mListener;

    public SwitchCompat(Context context) {
        super(context);
        mContext = context;
        init();
    }

    public SwitchCompat(Context context, AttrSet attrSet) {
        super(context, attrSet);
        mContext = context;
        init();
    }

    private void init() {
        contentView = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_switch_button, null, false);
        title = (Text) contentView.findComponentById(ResourceTable.Id_title);
        switchButton = (Switch) contentView.findComponentById(ResourceTable.Id_switch_button);
        switchButton.setCheckedStateChangedListener(new AbsButton.CheckedStateChangedListener() {
            @Override
            public void onCheckedChanged(AbsButton absButton, boolean bcc) {
                if (mListener != null) {
                    mListener.onCheckChange(absButton, bcc);
                }
            }
        });
        addComponent(contentView);
    }


    /**
     * OnCheckedChangeListener
     */
    public interface OnCheckedChangeListener {
        void onCheckChange(Component switchButon, boolean isChecked);
    }

    /**
     * setChecked
     * @param bcc bcc
     */
    public void setChecked(boolean bcc) {
        isCheched = bcc;
        switchButton.setChecked(isCheched);
    }

    /**
     * setOnCheckedChangeListener
     *
     * @param listener listener
     */
    public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
        mListener = listener;
    }
}
